package cn.edu.gduf.luowowo.article.service.impl;

import cn.edu.gduf.luowowo.article.domain.Destination;
import cn.edu.gduf.luowowo.article.domain.DestinationMonth;
import cn.edu.gduf.luowowo.article.domain.Region;
import cn.edu.gduf.luowowo.article.mapper.DestinationMapper;
import cn.edu.gduf.luowowo.article.mapper.ScenicPlaceMapper;
import cn.edu.gduf.luowowo.article.query.DestinationDaysQuery;
import cn.edu.gduf.luowowo.article.query.DestinationQuery;
import cn.edu.gduf.luowowo.article.query.DestinationSearchQuery;
import cn.edu.gduf.luowowo.article.service.IDestinationMonthService;
import cn.edu.gduf.luowowo.article.service.IDestinationService;
import cn.edu.gduf.luowowo.article.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.lang.reflect.Array;
import java.util.*;

@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationMonthService destinationMonthService;


    @Override
    public List<Destination> getDestsByDeep(int deep) {
        return destinationMapper.selectDestsByDeep(deep);
    }

    @Override
    public void saveOrUpdate(Destination destination) {

    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {

        //rid ==-1  表示查询国内所有省份

        if (rid == -1) {
            //id = 1; 表示中国id
            return destinationMapper.selectSubDestParentId(1L);
        }

        //区域
        Region region = regionService.get(rid);
        //关联的目的地id
        Long[] destIds = region.getRefIds();

        return destinationMapper.selectDestByIds(destIds);
    }


    @Override
    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(destinationMapper.selectForList(qo));
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        //parentId: 广州
        //: 根 >> 中国  >> 广东 >> 广州
        List<Destination> list = new ArrayList<>();
        createToast(parentId, list);
        Collections.reverse(list);  //列表反序
        return list;
    }

    //创建吐司
    private void createToast(Long parentId, List<Destination> list) {
        //广州
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if (destination == null) {
            return;
        }

        list.add(destination);

        if (destination.getParent() != null) {
            createToast(destination.getParent().getId(), list);
        }

    }

    @Override
    public Destination getCountry(Long id) {
        List<Destination> list = this.getToasts(id);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Destination getProvince(Long id) {
        //必须是国内的才设置省份
        List<Destination> list = this.getToasts(id);
        if (list != null && list.size() > 0) {
            //国外
            if (list.get(0).getId() != 1) {
                return null;
            }
        }

        if (list.size() > 1) {
            return list.get(1);
        }

       /* Destination destination = destinationMapper.selectByPrimaryKey(id);

        //国家
        if(destination.getDeep() == 1){
            return null;
        }

        //省份
        if (destination.getDeep() == 2){
            return destination;
        }


        //城市
        if (destination.getDeep() == 3){
            return destination.getParent();
        }*/

        return null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    @Override
    public void changeHotValue(Long id, int hot) {
        destinationMapper.updateHotValue(id, hot);
    }

    @Override
    public List<Destination> getHotDestByRegionId(Long regionId) {
        //regionId ==-1  表示查询国内所有省份
        if (regionId == -1) {
            //id = 1; 表示中国id
            return destinationMapper.selectSubDestParentId(1L);
        }
        //区域
        Region region = regionService.get(regionId);
        //关联的目的地id
        Long[] destIds = region.getRefIds();
        return destinationMapper.selectDestByIds(destIds);
    }

    @Override
    public Destination queryByStrategyId(Long strategyId) {
        return destinationMapper.selectByStrategyId(strategyId);
    }

    @Override
    public List<Destination> queryDestsForChinaCity() {
        return destinationMapper.selectCinaCitys();
    }

    @Override
    public List<Destination> queryByHotelThemeId(Long themeId) {
        return destinationMapper.selectByHotelThemeId(themeId);
    }

    @Override
    public List<Destination> queryForHotelCity() {
        List<Destination> destinations = new ArrayList<>();
        destinations = destinationMapper.selectForHotelCity();
        if (destinations.size() > 6) {
            destinations.subList(0, 6);
        }
        return destinations;
    }

    @Override
    public List<Destination> queryDestByMonId(Long monthId) {

        DestinationMonth destinationMonth = destinationMonthService.get(monthId);
        //关联的目的地id
        Long[] destIds = destinationMonth.getRefIds();
        return destinationMapper.selectDestByIds(destIds);

    }

    @Autowired
    private ScenicPlaceMapper scenicPlaceMapper;

    @Override
    public List<Destination> queryDest(DestinationSearchQuery qo) {

        List<Destination> list = destinationMapper.selectByThemeAndMonthAndDay(qo);

        /*for (Destination d : list) {
            ScenicPlace scenicPlace = scenicPlaceMapper.selectByPrimaryKey(d.getId());
            list.add(scenicPlace.getScenic());
        }*/

        return list;
    }



    @Override
    public List<Destination> queryDestDay(DestinationDaysQuery qo) {

        List<Destination> list = destinationMapper.selectDestDay(qo);
        return list;
    }



    @Override
    public List<Destination> queryHotFlights() {
        List<Destination> hotflights = destinationMapper.selectImportantDest();
        hotflights.addAll(destinationMapper.selectHotFlights());
        return hotflights;
    }

    @Override
    public List<Destination> queryFlightsByA2E() {
        return destinationMapper.selectFlightsByA2E();
    }

    @Override
    public List<Destination> queryFlightsByF2J() {
        return destinationMapper.selectFlightsByF2J();
    }

    @Override
    public List<Destination> queryFlightsByK2P() {
        return destinationMapper.selectFlightsByK2P();
    }

    @Override
    public List<Destination> queryFlightsByQ2W() {
        return destinationMapper.selectFlightsByQ2W();
    }

    @Override
    public List<Destination> queryFlightsByX2Z() {
        return destinationMapper.selectFlightsByX2Z();
    }

    @Override
    public Destination get(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }
}
